package com.hzhk.happyrent.ui.adapter.card;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzhk.happyrent.App;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.card.CardHotModel;
import com.hzhk.happyrent.ui.adapter.BaseAdapter;
import com.hzhk.happyrent.utils.CreateBitmapImage;
import com.hzhk.happyrent.utils.ListUtils;
import com.hzhk.happyrent.utils.glide.ShowImageUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by soft_zzc on 2017/11/25.
 */

public class CardHotAdapter extends BaseAdapter<CardHotModel.DataBean.CardsBean> {


    private Logger log = LoggerFactory.getLogger(this.getClass().getName());
    private CardHotModel cardHotModel;
    private static boolean isPosition;
    public CardHotAdapter(Context context) {
        super(context);
        isPosition = true;
        log.debug("jump into CardHotAdapter");
    }


    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_card_hot, null);
            viewHolder = new ViewHolder(convertView,position,mContext);
            viewHolder.setData(mList.get(position));
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.setData(mList.get(position));
        }
        return convertView;
    }

    class ViewHolder {
        private Logger log = LoggerFactory.getLogger(this.getClass().getName());
        @BindView(R.id.card_top_pic)
        ImageView cardTopPic;
        @BindView(R.id.card_hot_text1)
        TextView cardHotText1;
        @BindView(R.id.card_hot_text2)
        TextView cardHotText2;
        @BindView(R.id.card_hot_text3)
        TextView cardHotText3;
        @BindView(R.id.card_hot_text4)
        TextView cardHotText4;
        @BindView(R.id.card_hot_li)
        LinearLayout linearLayout;
        @BindView(R.id.card_hot_no)
        ImageView cardHotNo;
        @BindView(R.id.Re_card_hot_jump_into)
        LinearLayout relativeLayout;
        private Context context;
        int position;

        private String uil;

        ViewHolder(View view,int position,Context context) {
            this.position = position;

            this.context = context;
            ButterKnife.bind(this, view);
        }

        public void setData(final CardHotModel.DataBean.CardsBean cardsBean) {
            ShowImageUtils.showImageViewToCircleNoPlaceHolder(App.getContext(),cardsBean.getImage(),cardTopPic,0);
            cardHotText1.setText(cardsBean.getTitle());
            if(!ListUtils.isEmpty(cardsBean.getSelling_point())){
                String message = "";
                for (String s : cardsBean.getSelling_point()) {
                    message=message+s+"，";
                }
                cardHotText2.setText(message.substring(0,message.length()-1));
            }
            if(!ListUtils.isEmpty(cardsBean.getTags())){
                String message = "";
                for (String s : cardsBean.getTags()) {
                    message=message+s+"，";
                }
                cardHotText3.setText(message.substring(0,message.length()-1));
            }
            BitmapDrawable bd = new BitmapDrawable(CreateBitmapImage
                    .getInstance().getBitmap(linearLayout, "#3A89FF"));
            linearLayout.setBackgroundDrawable(bd);
            cardHotText4.setText(cardsBean.getBank_title());

            uil = cardsBean.getLink();
            log.debug(" position = " + position);

            switch (position ){
                case 0:
                    if(isPosition) {
                        cardHotNo.setVisibility(View.VISIBLE);
                        cardHotNo.setImageResource(R.drawable.card_hot_01);
                        isPosition = false;
                    }
                    break;
                case 1:
                    cardHotNo.setVisibility(View.VISIBLE);
                    cardHotNo.setImageResource(R.drawable.card_hot_02);
                    break;
                case 2:
                    cardHotNo.setVisibility(View.VISIBLE);
                    cardHotNo.setImageResource(R.drawable.card_hot_03);
                    break;
                default:
                    cardHotNo.setVisibility(View.GONE);
                    break;
            }

           /* relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, LoadWebViewActivity.class);

                    log.debug("uil  = " + cardsBean.getLink() );
                    intent.putExtra("uil",cardsBean.getLink());
                    intent.putExtra("name",cardsBean.getBank_title());
                    context.startActivity(intent);
                }
            });*/


        }
    }

}
